package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.One;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-10
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //Excel读取
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {

            InputStream in = file.getInputStream();
            EasyExcel
                    .read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService))
                    .sheet().doRead();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //查询所有课程分类树
    @Override
    public List<OneSubject> getAllSubject() {

        //获得所有一级分类
        QueryWrapper<EduSubject> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapper1);

        //获得所有二级分类
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapper2);

        //声明容器
        List<OneSubject> oneFinalSubject = new ArrayList<>();


        //遍历一级分类并封装
        for (EduSubject oneEduSubject: oneSubjectList) {
            //将一级分类封装
            OneSubject oneSubject = new OneSubject();
            //此时没有封装children
            BeanUtils.copyProperties(oneEduSubject,oneSubject);

            List<TwoSubject> twoFinalSubject = new ArrayList<>();

            //遍历二级分类
            for (EduSubject twoEduSubject: twoSubjectList) {

                //判断二级分类所属一级分类
                if (twoEduSubject.getParentId().equals(oneEduSubject.getId())){
                    //封装二级分类
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(twoEduSubject,twoSubject);
                    //将二级分类放入容器中
                    twoFinalSubject.add(twoSubject);
                }

            }
            //将二级分类设置到一级分类中
            oneSubject.setChildren(twoFinalSubject);
            //将一级分类放入容器中
            //此时包括children
            oneFinalSubject.add(oneSubject);

        }



        return oneFinalSubject;
    }
}
